﻿using UnityEngine;

namespace Game
{
    public abstract class SceneMovableEntity : SceneObjectEntity
    {
        public Bounds bounds { get; private set; }
        
        protected bool _flipX = false;
        protected bool _flipY = false;

        private SceneObjectShadowComponent _shadowComponent;

        public SceneMovableEntity(int id, SceneObjectEntityType entityType) : base(id, entityType)
        {
        }
        
        public override bool GetFlipX()
        {
            return _flipX;
        }

        public override bool GetFlipY()
        {
            return _flipY;
        }

        protected override void PostInstantiate()
        {
            BoxCollider2D boxCollider2D = _contentTransform.GetComponent<BoxCollider2D>();
            bounds = boxCollider2D.bounds;
            boxCollider2D.enabled = false;
            SceneSharedPropertiesComponent sharedPropertiesComponent = GetComponent<SceneSharedPropertiesComponent>();
            SceneObjectMoveComponent moveComponent = AddComponent<SceneObjectMoveComponent>();
            moveComponent.Setup(this, SceneConst.SceneObjectMoveSpeed);
            moveComponent.SetMoveBounds(sharedPropertiesComponent.sceneBounds);
            SceneObjectActionComponent actionComponent = AddComponent<SceneObjectActionComponent>();
            actionComponent.Setup(_contentTransform.GetComponent<Animator>());

            _shadowComponent = AddComponent<SceneObjectShadowComponent>();
            _shadowComponent.SetParent(transform);
            _shadowComponent.SetEntitySize(bounds.size);
        }
        
        protected override void OnInstantiateCompleted()
        {
            SetFlipX(_flipX);
            SetFlipY(_flipY);
            base.OnInstantiateCompleted();
        }
        
        protected override void UpdateSortingLayer(int layerId)
        {
            _shadowComponent.SetSortingLayer(layerId);
        }

        protected override void UpdateSortingOrder(int order)
        {
            _shadowComponent.SetSortingOrder(order - 9);
        }
    }
}